using Digitalmes.Application.Mes.Processes.StepTasks;
using Digitalmes.Application.Mes.Processes.StepTasks.Commands;
using Digitalmes.Application.Mes.Processes.StepTasks.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Processes;

/// <summary>
/// 工步终结点。
/// </summary>
public sealed class StepTaskEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/process", "stepTask")
            .RequireAuthorization()
            .MapGet(GetStepTaskListAsync, "list")
            .MapPost(CreateStepTaskAsync)
            .MapPost(CopyStepTaskAsync, "copy")
            .MapPut(UpdateStepTaskAsync, "{id}")
            .MapPut(ReorganizeStepTaskSeqAsync, "reorganize/{stepId}")
            .MapDelete(DeleteStepTaskAsync, "{id}");
    }

    [EndpointDescription("查询工步集合")]
    public async Task<IApiResult> GetStepTaskListAsync(ISender sender, [AsParameters] GetStepTaskListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建工步")]
    public async Task<IApiResult> CreateStepTaskAsync(ISender sender, ProcStepTaskDto dto)
    {
        var result = await sender.Send(new CreateStepTaskCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("复制工步")]
    public async Task<IApiResult> CopyStepTaskAsync(ISender sender, CopyStepTaskCommand command)
    {
        var result = await sender.Send(command);
        return result.ToApiResult();
    }

    [EndpointDescription("更新工步")]
    public async Task<IApiResult> UpdateStepTaskAsync(ISender sender, long id, ProcStepTaskDto dto)
    {
        var result = await sender.Send(new UpdateStepTaskCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("重组工步顺序号，顺序号从分组区间中按顺序重新排列")]
    public async Task<IApiResult> ReorganizeStepTaskSeqAsync(ISender sender, long stepId)
    {
        var result = await sender.Send(new ReorganizeStepTaskSeqCommand(stepId));
        return result.ToApiResult();
    }

    [EndpointDescription("删除工步")]
    public async Task<IApiResult> DeleteStepTaskAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteStepTaskCommand(id));
        return result.ToApiResult();
    }
}
